package com.okq.dao;

import com.okq.dao.base.Impl.BaseDao;
import com.okq.entity.Approval;
import com.okq.util.page.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述 : 审批Dao
 * 作者 : zdl
 * 日期 : 2017/6/30 9:47
 */
@Repository
public class ApprovalDao extends BaseDao {

    public void saveApproval(Approval approval) {
        super.save(approval);
    }

    public void deleteApproval(Approval approval) {
        super.delete(approval);
    }

    public void updateApproval(Approval approval) {
        super.update(approval);
    }

    public Approval getApprovalById(String id) {
        return (Approval) super.get(Approval.class, id);
    }

    public Page<Approval> pageByApproval(Page<Approval> page, Approval approval) {
        StringBuilder hqlSb = new StringBuilder();
        hqlSb.append("FROM Approval a WHERE a.isDelete = 0 ");
        if (approval != null) {
            Map<String, Object> param = new HashMap<>();
            Integer type = approval.getType();
            if (type != null && type != -1) {
                hqlSb.append(" AND a.type = ?1 ");
                param.put("1", type);
            }
            String applyName = approval.getApplyName();
            if (StringUtils.isNotBlank(applyName)) {
                hqlSb.append(" AND a.applyName LIKE ?2 ");
                param.put("2", "%" + applyName + "%");
            }
            String startTime = approval.getStartTime();
            if (StringUtils.isNotBlank(startTime)) {
                hqlSb.append(" AND a.applyTime >= ?3 ");
                param.put("3", startTime);
            }
            String endTime = approval.getEndTime();
            if (StringUtils.isNotBlank(endTime)) {
                hqlSb.append(" AND a.applyTime <= ?4 ");
                param.put("4", endTime);
            }
            hqlSb.append(" ORDER BY a.applyTime DESC ");
            return super.findByPage(page, hqlSb.toString(), param);
        }
        return page;
    }

    /**
     * 根据登录用户查询审批表list
     *
     * @param userId 登录用户Id
     * @return list
     */
    public List<Approval> listApprovalByUserId(String userId) {
        if (StringUtils.isNotBlank(userId)) {
            String hqlSb = "FROM Approval WHERE isDelete = 0 AND state = 0 " +
                    " And approvalId = '" + userId + "' " +
                    " ORDER BY approvalTime DESC ";
            return super.find(hqlSb);
        }
        return null;
    }

    /**
     * 根据订单产品Id或生产Id查询不同类型的审批
     *
     * @param type       类型
     * @param relevantId 相关Id
     * @return approval
     */
    public Approval getApprovalByTypeAndId(Integer type, String relevantId) {
        if (type != null && type != -1 && StringUtils.isNotBlank(relevantId)) {
            String hql = "FROM Approval WHERE isDelete = 0 AND type = " + type + " AND relevantId = '" + relevantId + "'";
            return (Approval) super.get(hql);
        }
        return null;
    }
}
